%% 计算每个区域内的数据点的q值
function [Q]=area_Q(DATA,HEX,track_locat,lat1,lat2,lon1,lon2,step) %步长a; %经纬度范围
%% 计算数据点的q值
for i=1:1:length(DATA) %遍历数据点 
    [v_next]=next_position1(i,DATA,HEX); %计算三个参数：v;nic;
    v=1-1/exp((abs(DATA(i,4)-v_next))/20);
    nic=1-DATA(i,5)/9;
    DATA(i,9)=0.2*v+0.8*nic; %计算对应的q值                
 end 

%% 轨迹断裂处补点
DATA_1=[];
[x,y]=line_point(track_locat(1,2:3),track_locat(1,4:5));
DATA_1(:,1)=x';DATA_1(:,2)=y';
 for i=2:1:length(track_locat)
     [x,y]=line_point(track_locat(i,2:3),track_locat(i,4:5));
     t=[x',y'];
     DATA_1=[DATA_1;t];
 end
DATA_1(:,3:8)=0;
 %% 补的点的q值
for i=1:1:size(DATA_1,1)  
    DATA_1(i,9)=1; %计算对应的q值               
end
DATA=[DATA;DATA_1];
%% 划分网格
[XMesh,~]=meshgrid(lon1:step:lon2,lat1:step:lat2); %划分网格
%% 计算所有网格的Q值和中误差
data1=zeros(1,3);
for i=1:size(DATA,1)
    data1(i,1)=ceil((DATA(i,1)-lon1)/step);
    data1(i,2)=ceil((DATA(i,2)-lat1)/step);
    data1(i,3)=DATA(i,9);
end
ZMesh=zeros(size(XMesh));Q=zeros(size(XMesh));
for i=1:size(data1,1)
    if data1(i,1)==0
        data1(i,1)=1;
    end
    if data1(i,2)==0
        data1(i,2)=1;
    end
end
for i=1:size(data1,1)
    ZMesh(data1(i,2),data1(i,1))=ZMesh(data1(i,2),data1(i,1))+1;
    Q(data1(i,2),data1(i,1))=Q(data1(i,2),data1(i,1))+data1(i,3);
end
Q=Q./ZMesh;
Q=Q(:,1:size(Q,2)-1);Q=Q(1:size(Q,1)-1,:);
end


